package com.csi.bumblebee.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

@Service
public class UserStorageServiceImpl implements com.csi.bumblebee.service.UserStorageService {

    @Autowired
    @Qualifier("jdkRedisTemplate")
    private RedisTemplate<String, Serializable> redisTemplate;

    /**
     * 设置storage过期时间,存放在redis中
     *
     * @param userId 用户id
     * @param day   过期时间(天)
     * @return 是否成功
     */
    @Override
    public boolean setExpTimeInRedis(String userId, double day) {

        redisTemplate.opsForValue().set("REDIS" + userId, "1", getExpireTime(day), TimeUnit.SECONDS);

        return redisTemplate.opsForValue().get("REDIS" + userId) != null;
    }

    /**
     * 获取redis中存放的storage过期时间
     *
     * @param userId 用户id
     * @return 过期时间(秒)
     */
    @Override
    public Long getExpTimeInRedis(String userId) {
        System.out.println(redisTemplate.getExpire("REDIS" + userId) + " *************** ");
        return redisTemplate.getExpire("REDIS" + userId);
    }

    /**
     * 天转秒
     * @param day 天数
     * @return 秒数
     */
    private long getExpireTime(double day) {
        return (long) (60L * 60 * 24 * day);
    }

}
